<?php

/**
 * Created by PhpStorm.
 * User: yangsen
 * Date: 15/6/30
 * Time: 下午1:34
 */

class User extends MY_Controller {
    function __construct() {
        parent::__construct();
    }

    public function registerAction() {
        $this->oauth->oauthcheck();
        $openid = $_SESSION['openid'];
        $mobile = $this->input->post('mobile');
        $identifierno = $this->input->post('identifierno');
        $realname = $this->input->post('realname');
        $expect = $this->input->post('expect');
        $canceldeal = $this->input->post('canceldeal');

        if($expect==null) {
            $expect = 2000;
        }

        $slogan = $this->input->post('slogan');
        $effect = $this->db->update('t_user',array('realname'=>$realname,'mobile'=>$mobile,'identifierno'=>$identifierno,
            'slogan'=>$slogan,'expect'=>$expect,'canceldeal'=>$canceldeal,'ctime'=>time()),array('openid'=>$openid));

        header('contentType:application/json;charset=utf-8');
        if($effect) {
            //根据身份证号码只能填写一次来算如果有身份证号码更新就算是有新用户注册.
            if(!empty($identifierno)) {
                $nowhour = date('YmdH', time());
                $this->db->query("update t_stat set regcnt=regcnt+1 where ctimehour=$nowhour");
            }
            echo json_encode(array('retcode'=>1,'retmsg'=>'更新资料成功','gotourl'=>base_url('/welcome/goods/1')));
            $_SESSION['userinfo']['mobile']=$mobile;
            $_SESSION['userinfo']['identifierno']=$identifierno;
        } else {
            echo json_encode(array('retcode'=>-1,'retmsg'=>'更新失败'));
        }
    }

    /**
     * 我的订单
     */
    public function myorder() {

        $this->oauth->oauthcheck();
        $openid = $_SESSION['openid'];

        $this->db->select('o.id as orderid,o.price,o.goodsid,g.picurl,o.status,g.title');
        $this->db->join('t_goods g', 'o.goodsid = g.id');
        $result = $this->db->get_where('t_order o',array('o.owner_openid'=>$openid));

        //echo $this->db->last_query();

        $result = $result->result_array();

        $data['orders'] = $result;

        $this->load->view('header');
        $this->load->view('myorder',$data);
    }

    /**
     * 新用户注册收费10元
     */
    public function createaccount() {

        $this->oauth->oauthcheck();
        $this->load->view('header');
        $this->load->view('createaccount');
    }

    /*
     * 设置个人相关信息
     */
    public function custom() {

        $this->oauth->oauthcheck();

        $member = new Member();
        $openid = $_SESSION['openid'];
        $member = $member->getUserInfoFromDb($openid);
//        $str = $this->db->last_query();
        $data['member']=$member;
        $this->load->view('header');
        $this->load->view('register',$data);
    }

    /**
     * 主题设置页面
     * @TODO
     */
    public function theme() {

    }

    public function myprogress() {
        $this->oauth->oauthcheck();
        $expect = $this->db->select('expect')->get_where('t_user',array('openid'=>$_SESSION['openid']))->row(0)->expect;
        $this->db->select('payer_msg,payer_openid,fee,p.ctime,u.nickname,u.headimgurl');
        $result = $this->db->join('t_user u','u.openid=p.payer_openid')->get_where('t_payrecord p',array('owner_openid'=>$_SESSION['openid'],'p.status'=>2));

        $result = $result->result_array();
        $data['myexpect']= $expect==null?1:$expect;
        $data['donatelist'] = $result;
        $openid = $_SESSION['openid'];
        $this->load->view('header');
        $this->load->view('sharepage',$data);
    }






}